package com.bitbakery.plugin.scheme.lang;

import com.bitbakery.plugin.scheme.lang.lexer.SchemeLexer;
import com.bitbakery.plugin.scheme.lang.lexer.SchemeTokenTypes;
import com.bitbakery.plugin.scheme.lang.psi.SchemeDefine;
import com.intellij.lang.cacheBuilder.DefaultWordsScanner;
import com.intellij.lang.cacheBuilder.WordsScanner;
import com.intellij.lang.findUsages.FindUsagesProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;

class SchemeFindUsagesProvider implements FindUsagesProvider {
    public boolean canFindUsagesFor(PsiElement psiElement) {
        return psiElement instanceof PsiNamedElement;
    }

    public String getHelpId(PsiElement psiElement) {
        return null;
    }

    @NotNull
    public String getType(PsiElement element) {
        if (element instanceof SchemeDefine) {
            return "define";
        }
        return "";
    }

    @NotNull
    public String getDescriptiveName(PsiElement element) {
        String name = ((PsiNamedElement) element).getName();
        return name != null ? name : "";
    }

    @NotNull
    public String getNodeText(PsiElement element, boolean useFullName) {
        return getDescriptiveName(element);
    }

    public WordsScanner getWordsScanner() {
        return new SchemeWordsScanner();
    }

    private static class SchemeWordsScanner extends DefaultWordsScanner {
        public SchemeWordsScanner() {
            super(new SchemeLexer(),
                    TokenSet.create(SchemeTokenTypes.DEFINE),
                    SchemeTokenTypes.COMMENTS,
                    SchemeTokenTypes.LITERALS);
        }
    }
}
